#include "I2C.h"

// 1010 000 [R/W] R=1 W=0
#define AT24C02_ADDR 0xA0

/**
  * @brief   写入一个字节
  * @details 
  * @param   wordAddr 写入的地址0~255
  * @param   byte 要写入的数据
  * @retval  
  */
void AT24C02_WriteByte(unsigned char wordAddr, unsigned char byte) {
	I2C_Start();
	I2C_SendByte(AT24C02_ADDR);
	I2C_ReceiveAck();
	I2C_SendByte(wordAddr);
	I2C_ReceiveAck();
	I2C_SendByte(byte);
	I2C_ReceiveAck();
	I2C_Stop();
}

/**
  * @brief   读取一个字节
  * @details 
  * @param   wordAddr 要读取的地址0~255
  * @retval  读取到的一个字节数据
  */
unsigned char AT24C02_ReadByte(unsigned char wordAddr) {
	unsigned char byte = 0;
	I2C_Start();
	I2C_SendByte(AT24C02_ADDR);
	I2C_ReceiveAck();
	I2C_SendByte(wordAddr);
	I2C_ReceiveAck();
	
	I2C_Start();
	I2C_SendByte(AT24C02_ADDR | 0x01);
	I2C_ReceiveAck();
	byte = I2C_ReceiveByte();
	I2C_SendAck(1);
	I2C_Stop();
	
	return byte;
}